package br.unicamp.ic.sgct.server.dominio.entidades;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import br.unicamp.ic.sgct.client.dominio.to.AvaliacaoTO;
import br.unicamp.ic.sgct.client.dominio.to.RevisaoTO;

@SuppressWarnings("serial")
@Entity
@Table(name="T_REVISAO")
public class Revisao implements Serializable {
	
	@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy = "revisao")
	Set<Avaliacao> avaliacoes;
	
	@Column(nullable = false)
	@Temporal(TemporalType.DATE)
	Date data;
	
	@Id
	@Column(name = "id_revisao")
	@TableGenerator(name = "REVISAO_GEN", allocationSize = 1, initialValue = 1, 
					table = "T_ID", pkColumnName = "key", valueColumnName = "valor",
					pkColumnValue = "ID_REVISAO")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "REVISAO_GEN")	
	long id;
	
	@OneToOne(optional = true, targetEntity = Submissao.class, 
  			  cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST})
  	@JoinColumn(name = "id_submissao", nullable = true)
	Submissao submissao;
	
	public Revisao() {
		super();
	}
	
	public RevisaoTO getTo(){
		RevisaoTO revisao = new RevisaoTO();
		
		List<AvaliacaoTO> avaliacoesto = new ArrayList<AvaliacaoTO>();
		
		revisao.setData(data);
		revisao.setId(id);
		//revisao.setSubmissao(submissao.getTo());
		
		for (Avaliacao avaliacao : avaliacoes) {
			avaliacoesto.add(avaliacao.getTo());
		}
		revisao.setAvaliacoes(avaliacoesto);
		
		return revisao;
	}
	
	public Set<Avaliacao> getAvaliacoes() {
		return avaliacoes;
	}
	public void setAvaliacoes(Set<Avaliacao> avaliacoes) {
		this.avaliacoes = avaliacoes;
	}
	public Date getData() {
		return data;
	}
	public void setData(Date data) {
		this.data = data;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public Submissao getSubmissao() {
		return submissao;
	}
	public void setSubmissao(Submissao submissao) {
		this.submissao = submissao;
	}
	
}
